package controllers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import play.data.validation.Required;
import models.Department;
import models.ReturnListVo;
import utils.BizConstants;
import vos.DepartSearchVo;
import vos.ReturnCode;

public class DepartmentController extends BaseController {
		
	/**
	 * 跳转到部门页面
	 *
	 * @Title: department 
	 * @return: void
	 */
	public static void department(){
		render();
	}
	/**
	 * 获取部门数据列表
	 *
	 * @Title: departmentList 
	 * @param page 页码
	 * @param pagesize 页大小
	 * @param departmentCode 部门编码
	 * @param departmentName 部门名称
	 * @return: void
	 */
	public static void departmentList(int page,int rows,String departmentCode,String departmentName){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		DepartSearchVo vo =new DepartSearchVo(page,rows,departmentCode,departmentName);
		long total=Department.countDepartment(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<Department> lists=Department.listDepartment(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 批量启用部门
	 *
	 * @Title: enableDepartment 
	 * @param id 部门id组
	 * @return: void
	 */
	public static void enableDepartment(String id){
		if(StringUtils.isEmpty(id)){
			renderFailedJson(ReturnCode.ErrorCode,"选中行为空");
		}
		try{
			Map<String ,List<String>> map=new HashMap<String,List<String>>();
			String[] ids=id.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, ids);
			map.put("listids", listid);
			Department.enableDepartment(map);
			renderFailedJson(ReturnCode.SuccessCode,"启用成功");
		}catch(Exception e){
			e.printStackTrace();
			renderFailedJson(ReturnCode.ErrorCode,"");
		}
		
	}
	/**
	 * 批量停用部门
	 *
	 * @Title: disableDepartment 
	 * @param id 部门id组
	 * @return: void
	 */
	public static void disableDepartment(String id){
		if(StringUtils.isEmpty(id)){
			renderFailedJson(ReturnCode.ErrorCode,"选中行为空");
		}
		try{
			Map<String ,List<String>> map=new HashMap<String,List<String>>();
			String[] ids=id.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, ids);
			map.put("listids", listid);
			Department.disableDepartment(map);
			renderFailedJson(ReturnCode.SuccessCode,"停用成功");
		}catch(Exception e){
			e.printStackTrace();
			renderFailedJson(ReturnCode.ErrorCode,"");
		}
		
	}
	/**
	 * 批量删除部门
	 *
	 * @Title: disableDepartment 
	 * @param id 部门id组
	 * @return: void
	 */
	public static void betchDeleteDepartment(String id){
		if(StringUtils.isEmpty(id)){
			renderFailedJson(ReturnCode.ErrorCode,"选中行为空");
		}
		try{
			Map<String ,List<String>> map=new HashMap<String,List<String>>();
			String[] ids=id.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, ids);
			map.put("listids", listid);
			Department.betchDeleteDepartment(map);
			renderFailedJson(ReturnCode.SuccessCode,"停用成功");
		}catch(Exception e){
			e.printStackTrace();
			renderFailedJson(ReturnCode.ErrorCode,"");
		}
		
	}
	/**
	 * 根据id删除数据
	 *
	 * @Title: deleteByid 
	 * @param id 部门id
	 * @return: void
	 */
	public static void deleteByid(String id){
		if(StringUtils.isEmpty(id)){
			renderFailedJson(ReturnCode.ErrorCode,"部门不能为空");
		}
		Department depart =Department.findById(id);
		if(depart==null){
			renderFailedJson(ReturnCode.ErrorCode,"部门存在");
		}
		Department.deleteDepartment(id);
	}
	/**
	 * 添加部门页面
	 *
	 * @Title: addDepartment 
	 * @return: void
	 */
	public static void addDepartment(){
		render();
	}
	/**
	 * 
	 *	验证部门编号是否存在
	 * @Title: checkCode 
	 * @param departmentCode 部门编号
	 * @return: void
	 */
	public static void checkCode(String departmentCode){
		if(StringUtils.isEmpty(departmentCode)){
			renderFailedJson(ReturnCode.ErrorCode,"部门编号为空");
		}
		Department depart=Department.checkCode(departmentCode);
		if(depart==null){
			renderFailedJson(ReturnCode.SuccessCode,"部门不存在");
		}
		renderFailedJson(ReturnCode.ErrorCode,"部门已存在");
	}
}
